﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRAdminUI.Profile
{
    public partial class SentMessage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["sender"] != null)
                {
                    txtReciverCode.Value = Session["sender"].ToString();
                    txtSubject.Value = Session["subject"].ToString();
                }
            }

            okImage.Visible = false;
        }

        protected void BtnSentMessage(object sender, EventArgs e)
        {
            try
            {
                DAL.DAO.Message message = new DAL.DAO.Message();
                MessageManager manager = new MessageManager();
                Code senderCode = new Code();
                senderCode.CodeName = Session["Code"].ToString();
                message.Sender = senderCode;
                message.SenderName = Session["name"].ToString();
                Code reciverCode = new Code();
                reciverCode.CodeName = txtReciverCode.Value;
                message.Recever = reciverCode;
                message.Subject = txtSubject.Value;
                message.MessageBody = txtMessage.Value.Replace(char.ConvertFromUtf32(13), "<br/>");
                message.Date = DateTime.Now.ToShortDateString();
                if (manager.SendMessage(message))
                {
                    msgLbl.ForeColor = Color.Green;
                    msgLbl.Text = "Message successfully Sent";
                    ClearInputs();
                }
                else
                {
                    msgLbl.ForeColor = Color.Green;
                    msgLbl.Text = "Message Not Sent";
                }
            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Green;
                msgLbl.Text = ex.Message;
            }

        }
        protected void ClearInputs()
        {

            txtReciverCode.Value = "";
            txtSubject.Value = "";
            txtMessage.Value = "";
        }
    }
}